﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WP09S03_OfficeTracking.Controller;

namespace WP09S03_OfficeTracking
{
    public partial class FrmEmployee : WP09S03_OfficeTracking.ParentClass.ParentForm
    {
        EmployeController controller;
        
        public FrmEmployee()
        {
            InitializeComponent();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            DiagAddEmpoyee dialog = new DiagAddEmpoyee();
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                SetMessage("1 record added");
                this.employeeControl.RefreshData();
            }  
        }

        private void buttonEdit_Click(object sender, EventArgs e)
        {
            if (this.employeeControl.dataGridViewEmployees.SelectedRows.Count > 0)
            {
                DataGridViewRow row = this.employeeControl.dataGridViewEmployees.SelectedRows[0];
                DiagAddEmpoyee dialog = new DiagAddEmpoyee(row.Cells["EmployeeID"].Value.ToString().Trim(),
                                                           row.Cells["EmployeeName"].Value.ToString().Trim(),
                                                           row.Cells["ContactNo"].Value.ToString().Trim(),
                                                           row.Cells["Email"].Value.ToString().Trim());
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    SetMessage("Record Updated");
                    this.employeeControl.RefreshData();
                }
            }
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            if (this.employeeControl.dataGridViewEmployees.SelectedRows.Count > 0)
            {
                DataGridViewRow row = this.employeeControl.dataGridViewEmployees.SelectedRows[0];
              
                string message = "Are you sure to delete employee record of " + row.Cells["EmployeeName"].Value.ToString() + "?";

                if (MessageBox.Show(message, "Confirm Deletion", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    
                    try {
                        controller.DeleteEmployee(row.Cells["EmployeeID"].Value.ToString(),
                                                           row.Cells["EmployeeName"].Value.ToString(),
                                                           row.Cells["ContactNo"].Value.ToString(),
                                                           row.Cells["Email"].Value.ToString());


                        this.employeeControl.RefreshData();
                        SetMessage("Record Deleted");
                      

                    }
                    catch(Exception exc)
                    {
                        SetMessage(exc.Message);
                    }
                }
            }
        }

        private void FrmEmployee_Load(object sender, EventArgs e)
        {
            controller = new EmployeController();
            this.employeeControl.RefreshData();
            int numRecords = this.employeeControl.dataGridViewEmployees.RowCount;
            SetMessage("Found " + numRecords + " Records.");

        }

        private void employeeControl_OnSearchResultEvent(int numRecords)
        {
            SetMessage("Found " + numRecords + " Records.");
        }
    }
}
